<?

//Data loading
$title='TABEL ACCOUNTING';
$perush = 'PT. AMPEL JAYA';
$page   = 'HALAMAN';

$DEFINE_FONT=0;
$pdf=new COAPDFReport("P","mm","Letter");
$pdf->AddPage();
$Result = COAList ("", "", "");
$pdf->COATable($Result);
$pdf->Output();


function SumAccount($skada,$mode) {
  global $DBConnection;
   $Year   = GetParam("y","");
   $LastYear = $Year-1;
   $Month  = GetParam("m","");
   if ($Month<10) $bulan = "0".$Month;
   if (($Month-1)<10) $lastbl = "0".($Month-1);

   if ($Month==1) $LastMonth = "awal";
   else $LastMonth = "aku".$lastbl."";

   $Mutasi = "(mutd".$bulan."-"."mutk".$bulan.")";
   $CurrentMonth = "aku".$bulan."";
   if ($mode=="mutasi" || $mode=="current" || $mode=="lastmonth") {
     $SQL = "
         SELECT SUM($Mutasi) as mutasi,SUM($CurrentMonth) as currentmonth,SUM($LastMonth) as lastmonth,concat(tblcoa.pathprn,'%'),skada
         FROM tblcoa
         WHERE tblcoa.pathprn like concat(tblcoa.pathprn,'%') AND ledger=$Year AND skada='$skada'
         GROUP BY skada
     ";
     if (GetParam("office","")) $SQL .= " AND ktrasl='".GetParam("office","")."'";
   //print $SQL."<p>\n";
   $Row = $DBConnection->dbc->get_row($SQL);
   //$DBConnection->dbc->debug();

   } elseif($mode=="lastyear") {
             $SQL = "
                 SUM($CurrentMonth) as currentmonth
                 FROM tblcoa
                 WHERE tblcoa.pathprn like concat(tblcoa.pathprn,'%') AND ledger=$LastYear
             ";
           if (GetParam("office",""))   $SQL .= " AND ktrasl='".GetParam("office","")."'";
           $Row = $DBConnection->dbc->get_row($SQL);
   $DBConnection->dbc->debug();
   }

   return $Row;
}

function COAList ($Space, $Index, $ParentID) {
            global $RS,$Index,$DBConnection;
            if (!$Index) $Index = 0;



            if (strlen($ParentID)) $WHERE = "WHERE parent='$ParentID' AND ktrasl='".GetParam("office","")."' AND (coatype='REVENUES' OR coatype='EXPENSES' OR coatype='OTHER EXPENSES') AND tblcoa.grup=tblcoagroup.group_id AND printing=1";
            else $WHERE = "WHERE ifnull(parent,'') = '' AND ktrasl='".GetParam("office","")."' AND (coatype='REVENUES' OR coatype='EXPENSES' OR coatype='OTHER EXPENSES') AND tblcoa.grup=tblcoagroup.group_id AND printing=1";
            $SQL = "SELECT skada,namacc,jumlah,printing,total FROM tblcoa, tblcoagroup ".$WHERE." ORDER BY skada";
            //print $SQL."<br><br>\n\n\n\n";die();
            $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
            if ($MyResult){
                foreach ($MyResult as $RowResult) {
                                        $SaveName = $RowResult["namacc"];
                    $RowResult["namacc"] = $Space.$RowResult["namacc"];

                    $RS[$Index] = $RowResult;
                    $Index++;
                    COAList ($Space."      ", $Index, $RowResult["skada"]);
                                        if ($RowResult["jumlah"]=='1') {
                                          $Row = SumAccount($RowResult["skada"],"mutasi");

                                          $RowResult["namacc"] = $Space."TOTAL ".$SaveName.":";
                                          $RowResult["currentmonth"] = $Row->currentmonth;
                                          $RowResult["mutasi"] = $Row->mutasi;
                                          $RowResult["lastmonth"] = $Row->lastmonth;
                                          $RS[$Index] = $RowResult;
                                          $Index++;
                                          $Index++;
                                        }
                }
            }
        return $RS;
}



function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return strtoupper(date("d F Y", mktime(0, 0, 0, $month+1, 0, $year)));

}
class COAPDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        $this->SetFont('arialn','',10);
            $this->Cell(200,5,$perush,0,1,'C',0);
            $this->Cell(200,5,$title,0,1,'C',0);
            $this->Cell(200,5,BuildPeriod (GetParam("y",""), GetParam("m","")),0,1,'C',0);
            $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);

            $this->SetLeftMargin(5);
            $this->Line(5,30,210,30);

            $this->Ln();
            $this->Ln();
        //Header
        //Column widths
        //Column widths

        $w=array(85,30,30,30,30);
        $this->SetFont('arialn','',10);
        $this->Cell($w[0],7,"DESKRIPSI",1,0,'C',0);
        $this->Cell($w[1],7,"S/D BLN LALU",1,0,'C',0);
        $this->Cell($w[2],7,"MUTASI",1,0,'C',0);
        $this->Cell($w[3],7,"S/D BLN INI",1,0,'C',0);
        $this->Cell($w[4],7,"LAST YEAR",1,0,'C',0);
        $this->Ln();
        $this->SetFont('arialn','',8);



    }

    function COATable($Result) {
        //Column widths
        $w=array(85,30,30,30,30);
        //Data

        foreach($Result as $row)
        {
            $this->Cell($w[0],6,$row["namacc"],'LR');
                $this->Cell($w[1],6,number_format($row["lastmonth"]),'LR',0,'R',0);
                $this->Cell($w[2],6,number_format($row["mutasi"]),'LR',0,'R',0);
                $this->Cell($w[3],6,number_format($row["currentmonth"]),'LR',0,'R',0);
                $this->Cell($w[3],6,number_format($row["lastyear"]),'LR',0,'R',0);
            $this->Ln();
        }
        //Closure line
        //$this->Cell(array_sum($w),0,'','T');
    }

    function Footer() {
        //Closure line
        $this->Cell(205,0,'','T');
    }

}


?>
